


import {
  get,
  post,
  put,
} from "./base_service"


/**
 * 佳宝优惠券相关接口
 *
 * allen  2023-12-19
 */



/**
* 
* 获取优惠券列表
* type:1 通用券 2 商品券 3 品类券
*/
const queryCouponList = (type, page = 1, limit = 10) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons?page=" + page + "&limit=" + limit + "&useType=" + type).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}

/**
* 
* 获取所有的优惠券，首页用，就不用传type了
*/
const queryAllCoupons = (page = 1, limit = 6) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons?page=" + page + "&limit=" + limit + "&type=1").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}

/**
* 
* 查新人券
*/
const queryNewPersonCoupons = (page = 1, limit = 3) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons?page=" + page + "&limit=" + limit + "&type=2").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })

}
/**
 * 
 * 获取商品可以用的券
 * productId
 */
const queryProductCoupon = (productId) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons?productId=" + productId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
* 
* 获取优惠券列表(在商品详情中)
* type:1 通用券 2 商品券 3 品类券
*/
const queryProductCouponList = (productId, type) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons?page=1&limit=40&productId=" + productId + "&useType=" + type + "&type=1").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
* 
* 领取优惠券
* {couponId:1}
*/
const receiveCoupon = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/coupon/receive", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 
 * 查看自己的优惠券列表
 * type:usable 可用  unusable 已使用  unusable 已过期
 */
const queryMyCouponList = (type, page = 1, limit = 10) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupon/list?page=" + page + "&limit=" + limit + "&type=" + type).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 *
 * 根据预订单号查询可用优惠券
 * 
 */
const queryOrderAbleCoupons = (preOrderNo) => {
  return new Promise((resolve, reject) => {
    get("/api/front/coupons/order/" + preOrderNo).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}




module.exports = {
  queryCouponList,
  receiveCoupon,
  queryMyCouponList,
  queryProductCouponList,//在商品详情中获取优惠券
  queryProductCoupon,
  queryOrderAbleCoupons,//根据预订单号，查询可以用优惠券
  queryAllCoupons,//查询所有的优惠券
  queryNewPersonCoupons,//只查新人券
}

